﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NearForums.Configuration;

namespace NearForums.DataAccess.Xml
{
    public abstract class BaseDataAccess
    {
        protected static readonly string UsersFile = "Users.xml";
        protected static readonly string GroupsFile = "Groups.xml";
        protected static readonly string CategoriesFile = "Categories.xml";
        protected static readonly string ForumsFile = "Forums.xml";
        protected static readonly string TopicsFile = "Topics.xml";
        protected static readonly string MessagesFile = "Messages.xml";
        protected static readonly string TopicFile = "Topics\\{0}.xml";
        protected static readonly string PagesFile = "Pages.xml";
        protected static readonly string PageFile = "Pages\\{0}.xml";
        protected static readonly string TemplatesFile = "Templates.xml";
        protected static readonly string TagsFile = "Tags.xml";
        protected static readonly string SettingsFile = "Settings.xml";

        protected static readonly string DateTimeFormat = "MM/dd/yyyy HH:mm:ss";

        protected string GetDataPath(string fileName)
        {
            string dataPath = SiteConfiguration.Current.AppSettings.DataPath;
            if (string.IsNullOrEmpty(dataPath))
            {
                dataPath = SiteConfiguration.PathResolver("~/App_Data/");
                SiteConfiguration.Current.AppSettings.DataPath = dataPath;
            }

            return dataPath + fileName;
        }

        protected string ToString(DateTime? date)
        {
            return date.HasValue ? date.Value.ToString(DateTimeFormat) : null;
        }

        protected DateTime? ToNullableTime(string date)
        {
            DateTime? time = null;
            if (!string.IsNullOrEmpty(date))
            {
                time = DateTime.ParseExact(date, DateTimeFormat, null);
            }
            return time;
        }

        protected string ToString(DateTime date)
        {
            return date.ToString(DateTimeFormat);
        }

        protected DateTime ToTime(string date)
        {
            return DateTime.ParseExact(date, DateTimeFormat, null);
        }

        protected string fileName;
    }
}
